package com.rocbrid.utils;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.UUID;

/*
腾讯云oss上传文件
 */
public class CosUploader {

    /*
    SecretId:AKID5JQPEqKvUwFPOCrTvBMSPAi6SdcNvISR
    SecretKey:KWcP1QcbR6arxtfGo7SddUK4auJsQtBF
     */
    // 配置参数（建议通过配置文件注入）
    private static final String SECRET_ID = "AKID5JQPEqKvUwFPOCrTvBMSPAi6SdcNvISR";
    private static final String SECRET_KEY = "KWcP1QcbR6arxtfGo7SddUK4auJsQtBF";
    private static final String BUCKET_NAME = "typora778-1302470382";
    private static final String REGION = "ap-shanghai";
    private static final String COS_URL = "https://" + BUCKET_NAME + ".cos." + REGION + ".myqcloud.com/";

    // 初始化COS客户端
    private static COSClient initCOSClient() {
        COSCredentials cred = new BasicCOSCredentials(SECRET_ID, SECRET_KEY);
        ClientConfig clientConfig = new ClientConfig(new Region(REGION));
        clientConfig.setHttpProtocol(HttpProtocol.https); // 强制使用HTTPS
        return new COSClient(cred, clientConfig);
    }


      // 上传图片核心方法
      public static String uploadImage(MultipartFile file) {
      COSClient cosClient = initCOSClient();
      try {
      // 生成唯一文件名（防止覆盖）
      String originalName = file.getOriginalFilename();
      String fileExt = originalName.substring(originalName.lastIndexOf("."));
      String key = "images/" + UUID.randomUUID() + fileExt;

     // 构建上传请求
      InputStream inputStream = file.getInputStream();
      ObjectMetadata metadata = new ObjectMetadata();
      metadata.setContentLength(file.getSize());
      metadata.setContentType(file.getContentType());

      PutObjectRequest request = new PutObjectRequest(BUCKET_NAME, key, inputStream, metadata);
      PutObjectResult result = cosClient.putObject(request);

      return COS_URL + key;
      } catch (Exception e) {
      throw new RuntimeException("文件上传失败：" + e.getMessage());
      } finally {
      cosClient.shutdown(); // 关闭客户端连接
      }
      }
/*
    public static void main(String[] args)throws FileNotFoundException {
        String localFilePath = "/Users/gaopeng/Pictures/Snipaste_2025-03-26_13-44-17.png";
        File localFile = new File(localFilePath);
        FileInputStream fileInputStream = new FileInputStream(localFile);
        COSClient cosClient = initCOSClient();
        try {
            // 生成唯一文件名（防止覆盖）
          //  String originalName = file.getOriginalFilename();
          //  String fileExt = originalName.substring(originalName.lastIndexOf("."));
            String key = "images/" + UUID.randomUUID();

            // 构建上传请求
            ObjectMetadata metadata = new ObjectMetadata();
            PutObjectRequest request = new PutObjectRequest(BUCKET_NAME, key, fileInputStream, metadata);
            PutObjectResult result = cosClient.putObject(request);


        } catch (Exception e) {
            throw new RuntimeException("文件上传失败：" + e.getMessage());
        } finally {
            cosClient.shutdown(); // 关闭客户端连接
        }
    }*/
}